using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Diagnostics;
using System.IO;


namespace SuperService
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }
        protected override void OnBeforeUninstall(System.Collections.IDictionary savedState)
        {
 	         base.Uninstall(savedState);
             ServiceController controller = new ServiceController("IPLogger");
             try
             {
                 TimeSpan timeout = TimeSpan.FromMilliseconds(3000);
                 controller.Stop();
                 controller.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
             }
             catch (Exception)
             {
                 // Could not stop the Service.
             }

        }

        /*protected override void OnAfterUninstall(System.Collections.IDictionary savedState)
        {
            base.OnAfterUninstall(savedState);

            string targetDir = Context.Parameters["TargetDir"]; // Must be passed in as a parameter

            if (targetDir.EndsWith("|"))
                targetDir = targetDir.Substring(0, targetDir.Length - 1);

            if (!targetDir.EndsWith("\\"))
                targetDir += "\\";

            if (!Directory.Exists(targetDir))
            {
                Debug.WriteLine("Target dir does not exist: " + targetDir);
                return;
            }

            string[] files = new[] { "File1.txt", "File2.tmp", "File3.doc" };
            string[] dirs = new[] { "Logs", "Temp" };

            foreach (string f in files)
            {
                string path = Path.Combine(targetDir, f);

                if (File.Exists(path))
                    File.Delete(path);
            }

            foreach (string d in dirs)
            {
                string path = Path.Combine(targetDir, d);

                if (Directory.Exists(d))
                    Directory.Delete(d, true);
            }

            // At this point, all generated files and directories must be deleted.
            // The installation folder will be removed automatically.
        }*/

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            ServiceController controller = new ServiceController("IPLogger");
            try
            {
                controller.Start();
            }
            catch (Exception ex)
            {
                String source = "SuperService Installer";
                String log = "Application";
                if (!EventLog.SourceExists(source))
                {
                    EventLog.CreateEventSource(source, log);
                }

                EventLog eLog = new EventLog();
                eLog.Source = source;

                eLog.WriteEntry(@"The service could not be started. Please start the service manually. Error: " + ex.Message, EventLogEntryType.Error);

            }
        }

        private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {

        }

        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {

        }
    }
}